import torch
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification

class NLPProcessor:
    def __init__(self):
        # 加载预训练模型和tokenizer
        self.tokenizer = AutoTokenizer.from_pretrained("bert-base-chinese")
        self.model = AutoModelForSequenceClassification.from_pretrained("bert-base-chinese")
        
        # 初始化文本分类和生成管道
        self.classifier = pipeline("text-classification", model=self.model, tokenizer=self.tokenizer)
        self.generator = pipeline("text-generation", model="gpt2")
    
    def process(self, text):
        """处理输入文本，返回处理后的文本"""
        # 1. 意图识别
        intent = self.classifier(text)
        
        # 2. 根据意图生成响应
        response = self.generate_response(text, intent)
        
        return response
    
    def generate_response(self, text, intent):
        """根据意图生成响应文本"""
        # 简单示例：根据意图生成不同响应
        if intent[0]['label'] == 'POSITIVE':
            return "很高兴听到你这么说！"
        else:
            return "我明白了，请继续。"